package shiftGrid;

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<List<Integer>> shiftGrid(int[][] grid, int k) {
        // grid[i][j] ---> grid[(i+1+k)/n%m][(j+k)%n]
        //  i*m+(j+1)*n
        int n = grid[0].length;
        int m = grid.length;
        int [][]arr = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                int num = (i*n+j+k)%(m*n);
                int indexI = num/n;
                int indexJ = num%n;
                arr[indexI][indexJ] = grid[i][j];
            }
        }
        List<List<Integer>> ans = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < arr[i].length; j++) {
                list.add(arr[i][j]);
            }
            ans.add(list);
        }
        return ans;
    }
}
